<template>
  <div class="asset-inventory-detail-page">
    <el-card>
      <div style="margin-bottom: 16px; font-weight: bold;">资产盘点明细管理</div>
      <el-form :inline="true" size="small" style="margin-bottom: 16px;">
        <el-form-item label="盘点任务">
          <el-select v-model="inventoryId" placeholder="请选择盘点任务" filterable clearable @change="fetchList">
            <el-option v-for="item in inventoryList" :key="item.id" :label="item.taskName" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="list" border stripe style="width: 100%;">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="assetId" label="资产" min-width="120">
          <template #default="scope">
            {{ assetMap[scope.row.assetId] || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="result" label="盘点结果" min-width="100">
          <template #default="scope">
            <el-tag :type="resultTagType(scope.row.result)">{{ resultLabel(scope.row.result) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="120" />
        <el-table-column prop="createTime" label="盘点时间" min-width="160" />
      </el-table>
      <div v-if="!inventoryId" style="color: #888; margin-top: 32px;">请选择盘点任务以查看明细</div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'

const inventoryList = ref([])
const inventoryId = ref(null)
const list = ref([])
const assetMap = ref({})
const fetchInventoryList = async () => {
  const res = await request.get('/asset/inventory/list')
  if (res.code === 1) {
    inventoryList.value = res.data
  }
}
const fetchAssetMap = async () => {
  const res = await request.get('/asset/list')
  if (res.code === 1) {
    const map = {}
    res.data.forEach(item => { map[item.id] = item.name })
    assetMap.value = map
  }
}
const fetchList = async () => {
  if (!inventoryId.value) {
    list.value = []
    return
  }
  const res = await request.get('/asset/inventoryDetail/list', { params: { inventoryId: inventoryId.value } })
  if (res.code === 1) {
    list.value = res.data
  }
}
const resultLabel = (result) => {
  switch (result) {
    case 1: return '正常'
    case 2: return '丢失'
    case 3: return '损坏'
    case 4: return '其他'
    default: return '-'
  }
}
const resultTagType = (result) => {
  switch (result) {
    case 1: return 'success'
    case 2: return 'danger'
    case 3: return 'warning'
    case 4: return 'info'
    default: return ''
  }
}
onMounted(() => {
  fetchInventoryList()
  fetchAssetMap()
})
</script>

<style scoped>
.asset-inventory-detail-page {
  padding: 24px;
}
</style> 